<template>
  <div class="dc-container">
    <el-dialog v-model="visible" width="60%"
      :fullscreen="false" :title="title" :close-on-click-modal="false"
      @open="onDialogOpen" @close="onDialogClose">
      <el-form ref="editForm" :model="form" :rules="rules" label-width="160px" :disabled="mode == 'view'">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item prop="configFlag" label="是否完成第一次配置">
              <el-switch v-model="form.configFlag" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="minLength" label="最小长度">
              <el-input-number v-model="form.minLength" :min="4" :max="20" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="includeLetter" label="是否包含字母">
              <el-switch v-model="form.includeLetter" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="includeNumeral" label="是否包含数字">
              <el-switch v-model="form.includeNumeral" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="includeSpecial" label="是否包含特殊字符">
              <el-switch v-model="form.includeSpecial" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="changeFirst" label="首次登录更换密码">
              <el-switch v-model="form.changeFirst" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="tryCount" label="登录失败次数后锁定">
              <el-input-number v-model="form.tryCount" :min="1" :max="100" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="unlockTime" label="自动解锁分钟">
              <el-input-number v-model="form.unlockTime" :min="1" :max="1440" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="allowLast" label="密码可以与之前的相同">
              <el-switch v-model="form.allowLast" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="expire" label="密码过期月数">
              <el-input-number v-model="form.expire" :min="1" :max="60" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="warnDay" label="密码更新提醒天数">
              <el-input-number v-model="form.warnDay" :min="1" :max="90" controls-position="right"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="watermark" label="安全水印">
              <el-switch v-model="form.watermark" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="cryptoFlag" label="传输加密">
              <el-switch v-model="form.cryptoFlag" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="cryptoIv" label="加密IV">
              <el-input v-model="form.cryptoIv" :maxLength="16" placeholder="请输入加密IV" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="remarks" label="备注信息">
              <el-input v-model="form.remarks" :maxLength="255" type="textarea" :rows="3" placeholder="请输入备注信息" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onSubmit" type="primary" v-show="mode != 'view'">保存</el-button>
          <el-button @click="onDialogClose" v-if="mode != 'view'">取消</el-button>
          <el-button @click="onDialogClose" v-if="mode == 'view'">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getSecurityById, saveSecurity } from '@/api/sys/security'
import BaseUI from '@/views/components/baseUI'

export default {
  extends: BaseUI,
  name: 'security-form',
  components: {},
  data() {
    return {
      // 操作模式 view/edit/add
      mode: 'add',
      form: this.initForm(),
      // 对话框属性变量
      visible: false,
      title: '安全配置',
      // 表单验证规则
      rules: {
        minLength: [{ required: true, message: '最小长度不能为空', trigger: 'blur' }],
        tryCount: [{ required: true, message: '登录失败次数后锁定不能为空', trigger: 'blur' }],
        unlockTime: [{ required: true, message: '自动解锁分钟不能为空', trigger: 'blur' }],
        expire: [{ required: true, message: '密码过期月数不能为空', trigger: 'blur' }],
        warnDay: [{ required: true, message: '密码更新提醒天数不能为空', trigger: 'blur' }],
        cryptoIv: [{ required: true, message: '加密IV不能为空', trigger: 'blur' }]
      }
    }
  },
  props: {
    // 权限
    permission: {
      type: Object
    }
  },
  methods: {
    // 必须实现的方法
    async onSubmit() {
      let validFlag = true

      // 验证主表表单
      await this.$refs.editForm.validate((valid) => {
        if (!valid) {
          validFlag = false
          return false
        }
      })

      if (validFlag) {
        this.doSave()
      }
    },
    // 必须实现的方法
    doSave() {
      this.setLoad()
      saveSecurity(this.form)
        .then((responseData) => {
          if (responseData.code == 100) {
            this.visible = false
            this.showMessage({
              type: 'success',
              msg: '保存成功'
            })
            this.$emit('save-finished')
          } else {
            this.showMessage(responseData)
          }
          this.resetLoad()
        })
        .catch((error) => {
          this.outputError(error)
        })
    },
    // 必须实现的方法
    getFormById(id) {
      this.setLoad()
      return new Promise((resolve) => {
        getSecurityById(id)
          .then((responseData) => {
            let form = {}
            if (responseData.code == 100) {
              form = responseData.data
            } else {
              this.showMessage(responseData)
            }
            this.resetLoad()
            resolve(form)
          })
          .catch((error) => {
            this.outputError(error)
          })
      })
    },
    // 必须实现的方法
    onDialogClose() {
      this.visible = false
    },
    // 必须实现的方法
    onDialogOpen() {
      this.$nextTick(() => {
        this.$refs.editForm.clearValidate()
      })
    },
    // 必须实现的方法 - 初始化表单数据
    initForm() {
      return {
        // 根据数据库字段生成前端对象属性
        configFlag: '0', // 是否完成第一次配置
        minLength: 8, // 最小长度
        includeLetter: '1', // 是否包含字母
        includeNumeral: '1', // 是否包含数字
        includeSpecial: '0', // 是否包含特殊字符
        changeFirst: '1', // 首次登录更换密码
        tryCount: 10, // 登录失败次数后锁定
        unlockTime: 30, // 自动解锁分钟
        allowLast: '0', // 密码可以与之前的相同
        expire: 3, // 密码过期月数
        warnDay: 30, // 密码更新提醒天数
        watermark: '0', // 安全水印
        cryptoFlag: '1', // 传输加密
        cryptoIv: '1234567890000000', // 加密IV
        remarks: null // 备注信息
      }
    },
    // 必须实现的方法 - 打开查看模式
    async openView(id) {
      this.mode = 'view'
      this.title = `查看安全配置`
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.visible = true
    },
    // 必须实现的方法 - 打开编辑模式
    async openEdit(id) {
      this.mode = 'edit'
      this.title = `修改安全配置`
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.visible = true
    },
    // 必须实现的方法 - 打开新增模式
    openAdd() {
      this.mode = 'add'
      this.title = `添加安全配置`
      this.form = this.initForm()
      this.visible = true
    },
    // 必须实现的方法 - 打开复制模式
    async openCopy(id) {
      this.mode = 'add'
      this.title = `添加安全配置`
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.form.id = null //把id设置为空，添加一个新的
      this.visible = true
    }
  }
}
</script>